class Solution {//leetcode2-两数相加
private:
    void push_back(ListNode* back,int num){
        ListNode* node=new ListNode(num);
        node->next=back->next;
        back->next=node;
    }
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* newhead=new ListNode;
        ListNode*tail=newhead,*cur1=l1,*cur2=l2;
        int carry=0;
        while(cur1||cur2||carry){
            int sum=carry;
            if(cur1!=nullptr){
                sum+=cur1->val;
                cur1=cur1->next;
            }
            if(cur2!=nullptr){
                sum+=cur2->val;
                cur2=cur2->next;
            }
            carry=sum/10;
            sum%=10;
            push_back(tail,sum);
            tail=tail->next;
        }
        ListNode* next=newhead->next;
        delete newhead;
        return next;
    }
};